import json
import time
import random

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import numpy as np

options = Options()
# options.add_argument('--headless') 
# options.add_argument('--blink-settings=imagesEnabled=false') 
# options.add_experimental_option("excludeSwitches", ["enable-automation"])
# options.add_experimental_option('useAutomationExtension', False) 
driver = webdriver.Chrome(options=options)
driver.maximize_window()

try:
    with open('cookies.txt', 'r') as f:
            cookies = json.load(f)
            
except FileNotFoundError:
    driver.get('https://bilibili.com')
    time.sleep(2)

    login_btn = driver.find_element(By.XPATH,"//div[@class='header-login-entry']")
    login_btn.click()

    while 1:
        try:
            avatar = driver.find_element(By.XPATH,"//a[@class='header-entry-mini']")
        except Exception as e:
            time.sleep(0.5)
        else:
            print('login done')
            break

    dictcookies = driver.get_cookies()#读取登录之后浏览器的cookies
    jsoncookies = json.dumps(dictcookies)#将字典数据转成json数据便于保存

    with open('cookies.txt','w') as f:#写进文本保存
        f.write(jsoncookies)
    print('cookies saved')
    driver.quit()

   
cnt = 0
while 1:
    options = Options()
    # options.add_argument('--headless') 
    # options.add_argument('--blink-settings=imagesEnabled=false') 
    # options.add_experimental_option("excludeSwitches", ["enable-automation"])
    # options.add_experimental_option('useAutomationExtension', False) 
    driver = webdriver.Chrome(options=options)
    driver.maximize_window()


    print('---启动浏览器---')
    try:
        with open('url.txt', 'r') as u:
            url = u.read()
    except FileNotFoundError:
        url = input('请输入你的页面地址:')

    driver.get(url) #TODO:改成你自己的动态页地址
    time.sleep(1)
    
    with open('cookies.txt', 'r') as f:
        cookies = json.load(f)
        for cookie in cookies:
            driver.add_cookie(cookie)
    driver.refresh()
    time.sleep(3)
    # news = driver.find_element(By.XPATH,
    #         "//ul[@class='right-entry']/li[3]")
    # news.click()
    print('---开始点赞---')
    time.sleep(1)
    driver.switch_to.window(driver.window_handles[-1])
    time.sleep(2)

    time0 = time_start = time.time()
    while 1:
        try:
            body = driver.find_element(By.XPATH,"//body") 
            like_btns = driver.find_elements(By.XPATH,
            "//div[@class='bili-dyn-action like']")
            for like_btn in like_btns:
                like_btn.click()
                time.sleep(1)
                cnt = cnt + 1
                print('已点赞:',cnt,'个')
                time0 = time.time()
        except Exception as e:
            body.send_keys(Keys.PAGE_UP) 
            time.sleep(0.3) 
            print('err')   
        else:    
            body.send_keys(Keys.PAGE_DOWN)  
            time.sleep(0.3)

        time1 = time.time()
        if (cnt > 200) or (time1-time_start > 600):  
            cnt = 0
            driver.quit()
            print('定时重启')
            break

